SWEP.PrintName = "PewPew"
SWEP.WorldModel = "models/weapons/w_rocket_launcher.mdl"
SWEP.Automatic = false
SWEP.AngleOffset = Angle( 0, 180, 0 )
SWEP.Offset = Vector( -15,0,3 )
-- Currently no support for class deriving.

function SWEP:PrimaryAttack( targetEnt )
	-- targetEnt is any entity that the player has locked onto.
	-- startPos and targetNormal may be set if targetEnt isn't valid.
	
	if self.Owner:GetMana() < 30 then return end
	
	if ( SERVER ) then
		self.Owner:SetMana( self.Owner:GetMana() - 30 )
	end
	
	local AimVec = self.Owner:GetFakeAimVector()
	if ( CLIENT ) then
		if self.Owner == LocalPlayer() then
			local m_x, m_y = gui.MousePos()
			AimVec = ScreenToVector( m_x, m_y )
		end
	end
	
	self:FireBullet( targetEnt, self:GetShootPos(), AimVec )
	self:NextAttack( CurTime() + 0.1 )
end

usermessage.Hook( "playerShot", function( data )
	local pl = data:ReadEntity()
	pl:GetWeapon():FireBullet( data:ReadEntity(), data:ReadVector(), data:ReadVector() )
end )

function SWEP:FireBullet( targetEnt, startPos, targetNormal )
	
	local pl = self.Owner
	
	if SERVER then
	
		if ValidEntity( targetEnt ) then
			tracer_end = targetEnt:LocalToWorld(targetEnt:OBBCenter()) - Vector(0,0,4) + Vector( math.random( -5,5 ), math.random( -5,5 ), math.random( -5,5 ) )
			timer.Simple( tracer_end:Distance(self:GetShootPos())/(400), function()
				local endPos
				if ValidEntity(targetEnt) then
					endPos = targetEnt:LocalToWorld( targetEnt:OBBCenter() )
				else
					endPos = startPos + targetNormal * 1024
				end
				local trRes = util.TraceLine( { start = startPos, endpos = endPos, filter = self.Owner } )
				for _,pl in pairs( player.GetAll() ) do
					if pl:GetPos():Distance( trRes.HitPos ) < 50 then
						pl:CarTakeDamage( 25 )
					end
				end
			end )
		end
			
		local pls = RecipientFilter()
		pls:AddAllPlayers()
		pls:RemovePlayer( pl )
		umsg.Start( "playerShot", pls )
			umsg.Entity( pl )
			umsg.Entity( targetEnt )
			umsg.Vector( startPos )
			umsg.Vector( targetNormal )
		umsg.End()
		
	else

		local scrNorm = ( GAMEMODE:GetFakeAngles():Forward()*-25 + pl:GetEyeTrace().HitPos - Vector(0,0,35) ) - EyePos()
		scrNorm:Normalize()
		local mx, my = gui.MousePos()
		
		local tracer_start = self:GetMuzzle()
		local tracer_end = self.Owner:EyePos() + targetNormal * 1024 /*+ Vector( math.random( -20,20 ), math.random( -20,20 ), math.random( -20,20 ) )*/
		if self.Owner != LocalPlayer then
			tracer_end = self:GetShootPos() + targetNormal * 1024 /*+ Vector( math.random( -20,20 ), math.random( -20,20 ), math.random( -20,20 ) )*/
		else
			if ValidEntity( targetEnt ) then
				-- If we have a target, lock onto them instead.
				tracer_end = targetEnt:LocalToWorld(targetEnt:OBBCenter()) - Vector(0,0,4) /*+ Vector( math.random( -5,5 ), math.random( -5,5 ), math.random( -5,5 ) )*/
				-- Make them blink to indicate that we "hit" them.
				timer.Simple( tracer_end:Distance(self:GetShootPos())/(400), function() targetEnt:SetBlinking( true ) end )
				timer.Simple( tracer_end:Distance(self:GetShootPos())/(400)+0.1, function() targetEnt:SetBlinking( false ) end )
			end
		end
		
		local trRes = util.TraceLine( { start = tracer_start, endpos = tracer_end, filter = { pl, pl.Buggy } } )
		
		local TastyTracer = EffectData()
		TastyTracer:SetStart( tracer_start )
		TastyTracer:SetOrigin( trRes.HitPos )
		TastyTracer:SetEntity( targetEnt )
		util.Effect( "tasty_rocket", TastyTracer )
		
	end
	
end

function SWEP:Think()
end

function SWEP:DrawHUD()

	local m_x, m_y = gui.MousePos()
	self:DrawCrosshair( m_x, m_y, 2 ) -- MouseX, MouseY, Speed at which the crosshair rotates.

end
